'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    const now = new Date();
    
    // 检查权限是否已存在
    const [existingPermissions] = await queryInterface.sequelize.query(
      `SELECT resource FROM permissions WHERE resource = 'file' LIMIT 1`
    );

    if (existingPermissions.length > 0) {
      console.log('文件权限已存在，跳过创建');
      return;
    }

    // 创建文件管理相关权限
    const permissions = [
      {
        id: require('uuid').v4(),
        resource: 'file',
        action: 'read',
        description: '文件管理 - 查看文件',
        module: 'file',
        module_name: '文件管理',
        is_system: false,
        sort_order: 800,
        created_at: now,
        updated_at: now,
      },
      {
        id: require('uuid').v4(),
        resource: 'file',
        action: 'create',
        description: '文件管理 - 上传文件',
        module: 'file',
        module_name: '文件管理',
        is_system: false,
        sort_order: 801,
        created_at: now,
        updated_at: now,
      },
      {
        id: require('uuid').v4(),
        resource: 'file',
        action: 'delete',
        description: '文件管理 - 删除文件',
        module: 'file',
        module_name: '文件管理',
        is_system: false,
        sort_order: 802,
        created_at: now,
        updated_at: now,
      },
      {
        id: require('uuid').v4(),
        resource: 'file',
        action: 'config',
        description: '文件管理 - 配置管理',
        module: 'file',
        module_name: '文件管理',
        is_system: false,
        sort_order: 803,
        created_at: now,
        updated_at: now,
      },
    ];

    await queryInterface.bulkInsert('permissions', permissions);
    console.log('文件权限创建成功');
  },

  down: async (queryInterface, Sequelize) => {
    await queryInterface.sequelize.query(
      `DELETE FROM permissions WHERE resource = 'file'`
    );
  },
};

